package com.tao.plan.controller;


import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.common.R;
import com.tao.plan.domain.pojo.PCountPlan;
import com.tao.plan.service.PCountPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/v1/countPlan")
public class CountPlanController {
	@Autowired
	private PCountPlanService pCountPlanService;

	/**
	 * 添加计数任务
	 */
	@PostMapping("/add")
	public R add(@RequestBody PCountPlan countPlan){
		countPlan.setUpdateTime(LocalDateTime.now());
		countPlan.setUid(Long.valueOf(BaseThreadLocal.getUid()));
		pCountPlanService.save(countPlan);
		return R.build().success();
	}

	/**
	 * 修改计数任务
	 */
	@PostMapping("/update")
	public R update(@RequestBody PCountPlan countPlan){
//		countPlan.setUpdateTime(LocalDateTime.now());
		pCountPlanService.updateById(countPlan);
		return R.build().success();
	}

	/**
	 * 删除计数任务
	 */
	@DeleteMapping("/delete/{id}")
	public R delete(@PathVariable("id") Long id){
		pCountPlanService.removeById(id);
		return R.build().success();
	}

	/**
	 * 查询计数任务, 按照status查询
	 */
	@GetMapping("/list/{status}")
	public R list(@PathVariable("status") Integer status){
		List<PCountPlan> list = pCountPlanService.listByStatus(status);
		return R.build().success().put("data",list);
	}

	/**
	 * 计数, 点击然后增加计数, 每天/只能更新一次
	 */
	@PostMapping("/incr/{id}")
	public R incr(@PathVariable("id") Long id){
		boolean flag = pCountPlanService.incrCount(id);
		return R.build().success().put("data",flag);
	}

	/**
	 * 任务完成, 任务终止的状态修改
	 */
	@PostMapping("/updateStatus/{id}")
	public R updateStatus(@PathVariable("id") Long id) {
		PCountPlan pCountPlan = new PCountPlan();
		pCountPlan.setId(id);
		pCountPlan.setStatus(1);
		pCountPlanService.updateById(pCountPlan);
		return R.build().success();
	}
}
